//
//  SZPictorialMainViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "SZPictorialMainViewController.h"
#import "PKRequestManager.h"
#import "CJPictorialMainTableViewCell.h"
#import "CJPictorialMainModel.h"
#import "CJPictorailImageListViewController.h"
#import <MJRefresh.h>
#import "UMSocial.h"
#import <MBProgressHUD.h>
#import "SZBaseNavController.h"
@interface SZPictorialMainViewController () <UITableViewDataSource,UITableViewDelegate,CJPictorialMainTableViewCellDelegate>


@property (nonatomic, strong) NSMutableArray *sourceArray;  //保存请求下来的数据

@property (nonatomic, assign) NSInteger page;    //刷新
@property (nonatomic, strong) MBProgressHUD *hud;  //菊花


@end

@implementation SZPictorialMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    
    _sourceArray = [NSMutableArray array];  //初始化数据源数组

    [self requestData];
    [self hudMore];
    
    //刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  
        _page = 1;
        [self requestData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page += 1;
        [self requestData];

    }];

}


//菊花
-(void)hudMore
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.labelText = @"休息一下";
    _hud.color = kRandomClolor;
    _hud.minSize = CGSizeMake(100, 100);
    _hud.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_hud];
    [_hud show:YES];
}

//请求数据
- (void)requestData {

    //http://morguo.com/forum.php?mod=threadgallery&androidflag=1&appfrom=ios&iosversion=3.13&page=1
    
    NSString *urlStr = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=threadgallery&androidflag=1&appfrom=ios&iosversion=3.13&page=%ld",_page];
    
    [PKRequestManager requestWithUrlString:urlStr parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
        [_hud hide:YES];
        
        if (_page == 1) {
            [_sourceArray removeAllObjects];
        }
        
        
        NSArray *lists = dataDic[@"data"][@"list"];
        for (NSDictionary *dic in lists) {
            CJPictorialMainModel *model = [[CJPictorialMainModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_sourceArray addObject:model];
        }

        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
      
      [self.tableView reloadData];   //刷新
        
    } error:^(NSError *error) {
        
    }];
    
    
}

//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


//分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _sourceArray.count;
}


//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *idenfitier = @"CJPictorialMainTableViewCell.h";
    
    CJPictorialMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfitier];
    if (cell == nil) {
        cell = [[CJPictorialMainTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:idenfitier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CJPictorialMainModel *model = _sourceArray[indexPath.row];
    cell.model = model;
    cell.delegate = self;
    
    
   // NSInteger tid = [model.tid integerValue];
    //点赞
    cell.bottomView.praiseButton.tag = 3000 + indexPath.row;
    [cell.bottomView.praiseButton addTarget:self action:@selector(praiseButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    cell.bottomView.moreButton.tag = 4000 + indexPath.row;
    [cell.bottomView.moreButton addTarget:self action:@selector(moreButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    return cell;
}


//返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScreenWidth + 180;
}


//cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    CJPictorialMainModel *model = _sourceArray[indexPath.row];
  
    self.pictorialMainBlock(model.tid);

}

//cell的代理方法 获取点击的button
- (void)clickCell:(CJPictorialMainTableViewCell *)cell buttonTag:(NSInteger)tag {
    
    //获取cell的下表
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    
     CJPictorialMainModel *model = _sourceArray[index.row];
    NSMutableArray *array = model.imagelist;

    CJPictorailImageListViewController *imageListVC =
    [[CJPictorailImageListViewController alloc] init];
    
    //传值图片数组
    imageListVC.imageListArray = array;
    imageListVC.numOfSize = tag;
        
    [self presentViewController:imageListVC animated:NO completion:nil];

}


//点赞
- (void)praiseButton:(UIButton *)button {
    
    
    // 是否登录userName isEqualToString:@"YES"]
    if ([SZLoginTool shareLogin].isLogin) {
        
        button.selected = !button.selected;
        
        NSString *isLike = nil;
        if (button.selected) {
            isLike = @"like";
        } else {
            isLike = @"unlike";
        }
        
        
        CJPictorialMainModel *model = _sourceArray[button.tag - 3000];
        
        NSString *str = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=threadlikecp&op=%@&tid=%@&formhash=%@&androidflag=1&appfrom=ios&iosversion=3.13",isLike,model.tid, [SZLoginTool shareLogin].formhash];
        
        [PKRequestManager requestWithUrlString:str parDic:nil method:GET finish:^(NSDictionary *dataDic) {
            if (dataDic[@"data"][@"result"]) {
                SZLog(@"点赞成功");
                
                model.isliked = button.selected;
            } else {
                SZLog(@"点赞失败");
                button.selected = !button.selected;
            }
            
            
        } error:^(NSError *error) {
        button.selected = !button.selected;
        }];
        
    } else {
        
        SZLoginViewController *loginVC = [[SZLoginViewController alloc] init];
        SZBaseNavController *loginNav = [[SZBaseNavController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNav animated:YES completion:nil];
        
        
    }
}

//分享
- (void)moreButton:(UIButton *)button {
    
    CJPictorialMainModel *model = _sourceArray[button.tag - 4000];
    NSString *url = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=viewthread&tid=%@&isfrommobile=1",model.tid];
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *viewController = window.rootViewController;
    [UMSocialSnsService presentSnsIconSheetView:viewController
                                         appKey:@"569ee21fe0f55a76b10021e3"
                                      shareText:[NSString stringWithFormat:@"<<%@>>详情点击%@",model.subject,url]
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToEmail,UMShareToWechatFavorite,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToTencent,nil]
                                       delegate:nil];
    
}














@end
